package com.spring.common.taglib;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.log4j.Logger;

import com.spring.common.pojo.PageResult;
import com.spring.common.util.AppPropertiesUtil;

 
/**
 * @author lilongshun
 * @version 1.0.0
 * @date  Oct 15, 2010 10:33:02 AM
 * 分页标签 V2.0
 * 
 */
public class TagPage extends TagSupport {
	Logger log = Logger.getLogger(TagPage.class);
	// 接收表单别名
	private String formName = null;
	private String pageSize =null;
	private int maxSize=200;
	private String validate;
	
	//常量
	
	public String getValidate() {
		return validate;
	}

	public void setValidate(String validate) {
		this.validate = validate;
	}

	public String getFormName() {
		return formName;
	}

	public void setFormName(String formName) {
		this.formName = formName;
	}

	@Override
	public int doEndTag() throws JspException {
		PageResult pageResult = null;
		String DEFAULT_PAGE_SIZE=AppPropertiesUtil.getPropertyString("default.page.size");
		// 尝试从request中取出表单Bean
		pageResult = (PageResult) pageContext.getRequest().getAttribute(formName);
		if (pageResult == null) {
			pageResult = (PageResult)pageContext.getRequest().getAttribute(formName);
		}
		if (pageResult != null) {
			if(!pageResult.getIsPaging()){
				return EVAL_PAGE;
			}
			HttpServletRequest request=(HttpServletRequest)pageContext.getRequest();
			StringBuffer buffer=new StringBuffer();
			
			String path_xxx = request.getContextPath();
			Integer current=pageResult.getCurrent();
			Integer totalPage=pageResult.getTotalPage();
			
			StringBuffer bufferHide=new StringBuffer();
			bufferHide.append("\"<input type='hidden' name='pageSize' id='pageSize' value='"+DEFAULT_PAGE_SIZE+"' />");
			bufferHide.append("<input type='hidden' name='current' id='current' value='"+current+"' /> ");
			bufferHide.append("<input type='hidden' name='totalPage' id='totalPage' value='"+totalPage+"' />\"");
			
			buffer.append("<script src='"+path_xxx+"/js/spring.pager.js'></script>");
			buffer.append("<script type=\"text/javascript\">");
			buffer.append("$(function() {");
			buffer.append("if($('.form_s').length>0){$('.form_s').eq(0).append(").append(bufferHide.toString()).append(" );}");
			buffer.append("else{ $('form').eq(0).append(").append(bufferHide.toString()).append(" );}");
			buffer.append("$('#pager').pager({pagenumber : $('#current').val(),pagecount : $('#totalPage').val(),buttonClickCallback : PageClick});");
			buffer.append("});");
			
			buffer.append("var PageClick = function(pageclickednumber) {document.forms[0].reset();$('#current').val(pageclickednumber);document.forms[0].submit();}");
			buffer.append("</script>");
			//总页数不大于1，不显示分页标签
			String showP="";
			if(totalPage<2){
				showP=" display:none;";
			}
			buffer.append("<nav id=\"pager\" style=\" "+showP+"\"></nav>");			
			try {
					pageContext.getOut().print(buffer.toString()); 
					buffer.delete(0, buffer.length());
					pageResult=null;					
				} catch (Exception e) {
					log.error("分页控件出现异常"+e.getMessage());
				}
		}else{
			StringBuffer buffer = new StringBuffer();
			buffer.append("<input type='hidden' name='current' id='current' value='1'>");
			if(pageSize!=null){
				buffer.append("<input type='hidden' name='pageSize' id='pageSize' value='"+this.getPageSize()+"' />");
			}
			try {
				pageContext.getOut().print(buffer.toString()); 
				buffer.delete(0, buffer.length());
				pageResult=null;					
			} catch (Exception e) {
				log.error("分页控件出现异常"+e.getMessage());
			}
			
			//bootstrap start
//			buffer.append("<nav align='right'>");
//			buffer.append("<ul class=\"pagination\">");
//			buffer.append("<li class=\"disabled\"><a href=\"#\" aria-label=\"Previous\"><span aria-hidden=\"true\">&laquo;</span></a></li>");
//			buffer.append("<li class=\"active\"><a href=\"#\">1 <span class=\"sr-only\">(current)</span></a></li>");
//			buffer.append("<li><a href=\"#\">2</a></li>");
//			buffer.append("<li><a href=\"#\">3</a></li>");
//			buffer.append("<li><a href=\"#\">4</a></li>");
//			buffer.append("<li><a href=\"#\" aria-label=\"Next\"><span aria-hidden=\"true\">&raquo;</span></a></li>");
//			buffer.append("</ul>");
//			buffer.append("</nav>");
			//bootstrap end
			
		}
		return EVAL_PAGE;
	}

	public int getMaxSize() {
		return maxSize;
	}

	public void setMaxSize(int maxSize) {
		this.maxSize = maxSize;
	}

	/**
	 * @return the pageSize
	 */
	public String getPageSize() {
		return pageSize;
	}

	/**
	 * @param pageSize the pageSize to set
	 */
	public void setPageSize(String pageSize) {
		this.pageSize = pageSize;
	}

	
}
